<?php
namespace Home\Controller;
use Think\Controller;
class WeixinController extends Controller {
    public function index(){
        $redirect_uri = urlencode('http://'.$_SERVER['SERVER_NAME'].U('home/weixin/callback'));
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        redirect($url);
    }

    public function callback(){
        $code = $_GET['code'];
        $login_access_token_api = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.APPSECRET.'&code='.$code.'&grant_type=authorization_code';
        $login_access_token_data = json_decode(http_get($login_access_token_api));

        $login_access_token = $login_access_token_data->access_token;
        $openid = $login_access_token_data->openid;

        $user_info_api = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$login_access_token.'&openid='.$openid.'&lang=zh_CN';
        $userinfo = json_decode(http_get($user_info_api),true);
        M('user')->add(array(
        	'openid'=>$openid,
        	'nickname'=>$userinfo['nickname'],
        	'sex'=>$userinfo['sex'],
        	'country'=>$userinfo['country'],
        	'province'=>$userinfo['province'],
        	'city'=>$userinfo['city'],
        	'headimgurl'=>$userinfo['headimgurl'],
        ));
        $this->assign('userinfo',$userinfo);
        $this->display();
    }

    public function groupSend(){
    	$data = '{
		   "touser": [
		        "oHzM4wiC8Uw6gWdEwDWkkOFxp3mc", 
		        "oHzM4wvxnta3WDfY8sO7ZW7RIWDU" 
		    ], 
		    "msgtype": "text", 
		    "text": { 
		        "content": "小朋友，来玩啊."
		    }
		}';
		$url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.getAccessToken();
		http_post($url,$data);
    	
    }

    public function createNews(){
    	$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.getAccessToken();
    	$data = ' {
           "touser":"oHzM4wvxnta3WDfY8sO7ZW7RIWDU",
           "template_id":"sZNXV04kT795Ksf3IbwiRms_Z_IrDOpAuqC1IKkE-eE",
           "url":"http://weixin.qq.com/download",            
           "data":{
                   "first": {
                       "value":"恭喜你签到成功！",
                       "color":"#173177"
                   },
                   "keyword1":{
                       "value":"收尸",
                       "color":"#173177"
                   },
                   "keyword2": {
                       "value":"阳间",
                       "color":"#173177"
                   },
                   "keyword3": {
                       "value":"2016年9月22日",
                       "color":"#173177"
                   },
                   "keyword4": {
                       "value":"2016年9月23日",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"欢迎再次签到！",
                       "color":"#173177"
                   }
           }
       }';
    	http_post($url,$data);
    }
}